# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from ssc import settings
import pymysql, datetime, logging

prev_time = datetime.datetime.now() - datetime.timedelta(minutes=1)

class SscPipeline(object):

    def __init__(self):
        self.connect = pymysql.connect(
            host=settings.MYSQL_HOST,
            db=settings.MYSQL_DBNAME,
            user=settings.MYSQL_USER,
            passwd=settings.MYSQL_PASSWD,
            port=settings.MYSQL_PORT,
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor,
            use_unicode=True
        )
        self.cursor = self.connect.cursor()

    def process_time(self, index):
        str_index = '0{}'.format(index) if index < 10 else '{}'.format(index)
        str_time = prev_time.strftime("%Y-%m-%d %H:%M:") + str_index
        return str_time


    def process_item(self, item, spider):
        if(spider.name == 'qq'):
            try:
                for index, value in enumerate(item['qq_arr']):
                    str_time = self.process_time(index)
                    self.cursor.execute(
                        '''insert into qq(value, time, record_time)
                          value (%s,%s,%s)''',
                        (value,
                         str_time,
                         item['qq_time']))
                self.connect.commit()

            except Exception as error:
                logging.info(error)

        if(spider.name == 'tecent'):
            try:
                self.cursor.execute(
                    '''insert into tecent(value, time)
                      value (%s,%s)''',
                    (item['tecent_value'],
                     item['tecent_time']))
                self.connect.commit()

            except Exception as error:
                logging.info(error)

        return item
